package cn.ecnu.controller;

import cn.ecnu.common.utils.JwtToken;
import cn.ecnu.common.utils.R;
import cn.ecnu.mapper.*;
import cn.ecnu.pojo.*;
import cn.ecnu.service.ConsultantService;
import cn.ecnu.service.SupervisorService;
import cn.ecnu.vo.Converter;
import cn.ecnu.vo.fromFront.ChangeConsultantVo;
import cn.ecnu.vo.fromFront.ChangeDefaultDutyVo;
import cn.ecnu.vo.fromFront.ChangeSupervisorVo;
import cn.ecnu.vo.fromFront.SupervisorForm;
import cn.ecnu.vo.toFront.*;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static cn.ecnu.common.constant.BusinessConstant.CONSULTANT_DEFAULT_DUTY_KEY;
import static cn.ecnu.common.constant.BusinessConstant.SUPERVISOR_DEFAULT_DUTY_KEY;

@RestController
@RequestMapping("/supervisor")
public class SupervisorController {

    @Resource
    private SupervisorMapper supervisorMapper;

    @Resource
    private SupervisorService supervisorService;

    @Resource
    private HelpRecordMapper helpRecordMapper;

    @Resource
    private ConsultantMapper consultantMapper;

    @Resource
    private ConsultantService consultantService;

    @Resource
    private ConsultRecordMapper consultRecordMapper;

    @Resource
    private RedisTemplate<Object, Object> redisTemplate;

    @Resource
    private Converter converter;

    @GetMapping("/getAllSupervisors")
    public R getAllSupervisors() {
        try {
            List<Supervisor> supervisors = supervisorMapper.getAllSupervisor();
            ArrayList<SupervisorVo> result = new ArrayList<>();
            for (Supervisor supervisor : supervisors) {
                result.add(converter.getSupervisorVo(supervisor.getSupervisorId()));
            }
            return R.ok().put("data", result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    @GetMapping("/getSupervisorById/{id}")
    public R getSupervisorById(@PathVariable("id") Integer id) {
        try {
            SupervisorVo vo = converter.getSupervisorVo(id);
            return R.ok().put("data", vo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    @GetMapping("/getHelpRecords/{id}")
    public R getHelpRecords(@PathVariable("id") Integer id) {
        try {
            List<HelpRecord> records = helpRecordMapper.getHelpRecordBySupervisorId(id);
            ArrayList<HelpRecordVo> result = new ArrayList<>();
            for (HelpRecord record : records) {
                result.add(converter.getHelpRecordVo(record.getHelpRecordId()));
            }
            return R.ok().put("data", result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    @GetMapping("/getRelatedOnlineConsultants/{id}")
    public R getRelatedOnlineConsultants(@PathVariable("id") Integer id) {
        try {
            //有关联的那些咨询师
            List<Consultant> consultants = consultantMapper.getConsultantBySupervisorId(id);
            //筛选出在线的
            ArrayList<ConsultantVo> result = new ArrayList<>();
            for (Consultant consultant : consultants) {
                Integer consultantId = consultant.getConsultantId();
                if (consultantService.judgeOnline(consultantId)) {
                    result.add(converter.getConsultantVo(consultantId));
                }
            }
            return R.ok().put("data", result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    @GetMapping("/getRelatedConsultRecords/{id}")
    public R getRelatedConsultRecords(@PathVariable("id") Integer id) {
        try {
            List<ConsultRecord> records = consultRecordMapper.getConsultRecordBySupervisorId(id);
            ArrayList<ConsultRecordVo> result = new ArrayList<>();
            for (ConsultRecord record : records) {
                result.add(converter.getConsultRecordVo(record.getConsultRecordId()));
            }
            return R.ok().put("data", result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    @PostMapping("/add")
    public R add(@RequestBody SupervisorForm supervisorForm) {
        supervisorService.addSupervisor(supervisorForm);
        return R.ok();
    }

//    @JwtToken
    @PostMapping("/changeDefaultDuty")
    public R changeDefaultDuty(@RequestBody ChangeDefaultDutyVo vo) {
        supervisorService.changeDefaultDuty(vo);
        return R.ok();
    }

    @GetMapping("/getDefaultDuty/{id}")
    public R getDefaultDuty(@PathVariable("id") Integer id) {

        try {

            HashMap<Object, Object> map = new HashMap<>();

            SetOperations<Object, Object> opsForSet = redisTemplate.opsForSet();

            ArrayList<Integer> defaultDuty = new ArrayList<>();
            for (int day = 1; day <= 7; day++) {
                if (opsForSet.isMember(String.format(SUPERVISOR_DEFAULT_DUTY_KEY, day), id)) {
                    defaultDuty.add(day);
                }
            }

            map.put("defaultDuty", defaultDuty);

            return R.ok().put("data", map);

        } catch (Exception e) {

            e.printStackTrace();

        }

        return R.error();

    }

    @PostMapping("/changeConsultant")
    public R changeConsultant(@RequestBody ChangeConsultantVo vo) {
        supervisorService.changeConsultant(vo);
        return R.ok();
    }


}
